<template>
  <div>
    <div class="AnalysisContent">
      <el-descriptions class="AnalysisDescriptions" :column="1" border :labelStyle="{'text-align': 'center'}" style="margin-bottom:20px;">
        <el-descriptions-item>
          <template slot="label">{{$t('自动备份')}}</template>
          <el-switch :width="lang === 'zh'?56:70" v-model="autoSwitch" @change="setUpDown()" class="switch" style="margin-left: 20px;" active-color="#009966" inactive-color="#CCCCCC" active-value="on" inactive-value="off" :active-text="$t('开启')" :inactive-text="$t('关闭')"></el-switch>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">{{$t('备份周期')}}</template>
          <el-radio-group v-model="internal" style="margin-left:20px;" @change="setUpDown()">
            <el-radio label="day">{{$t('每天')}}</el-radio>
            <el-radio label="week">{{$t('每周')}}</el-radio>
          </el-radio-group>
        </el-descriptions-item>
      </el-descriptions>
      <el-button type="danger" size='small' @click="deleteData">{{$t('删除')}}</el-button>
      <el-table size='mini' ref="multipleTable" :data="tableList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" align='center' width="55"></el-table-column>
        <el-table-column prop="filename" align='center' :label="$t('文件名')" min-width="270">
          <template slot-scope="scope">
            {{scope.row}}
          </template>
        </el-table-column>
        <el-table-column align='center' width='200' fixed="right" :label="$t('操作')">
          <template slot-scope="scope">
            <el-button type="success" plain size="mini" @click="downloadFile(scope.row)">{{$t('下载')}}</el-button>
            <el-button type="danger" plain size="mini" @click="reFile(scope.row)">{{$t('还原')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <DownloadDialog v-if="downloadDialogVisible" ref="refDownloadDialogVisible"></DownloadDialog>
  </div>
</template>
<script>
import { getConfig, setConfig, reduction, delBackup } from '@/api/system/autoBackup'
import DownloadDialog from './components/DownloadDialog'
export default {
  name: 'Analysis',
  components: {
    DownloadDialog
  },
  data () {
    return {
      downloadDialogVisible: false,
      lang: this.$i18n.locale,
      tableList: [],
      autoSwitch: 'off',
      internal: 'day',
      nameStr: ''
    }
  },
  created () {
    this.listData()
  },
  methods: {
    listData () {
      getConfig().then(res => {
        this.autoSwitch = res.info[0].switch
        this.internal = res.info[0].internal ? res.info[0].internal : 'day'
        this.tableList = res.info[0].filelist
      })
    },
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item
        } else {
          nameStr = nameStr + '@#@' + item
        }
      }
      this.nameStr = nameStr
    },
    setUpDown () {
      setConfig({switch: this.autoSwitch, internal: this.internal}).then(res => {
        this.$messages.success(this.$t('设置成功'))
      })
    },
    // 下载
    downloadFile (data) {
      this.downloadDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refDownloadDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delBackup({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$messages.success(this.$t('删除成功'))
            }
            this.nameStr = []
            this.listData()
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    reFile (data) {
      this.$confirm(this.$t('确定要还原吗？还原需要设备重启！'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        reduction({ name: data }).then(res => {
          if (res.status === 0) {
            this.$messages.success(this.$t(res.msg))
            this.listData()
          }
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
.AnalysisContent {
  width: 100%;
  margin: 0 auto;
  overflow-x: auto;
}
.AnalysisDescriptions {
  width: 700px;
  margin: 0 auto;
}
</style>
